package org.dromara.stock.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.stock.domain.bo.AttentionStockBo;
import org.dromara.stock.domain.vo.AttentionStockVo;
import org.dromara.stock.domain.AttentionStock;
import org.dromara.stock.mapper.AttentionStockMapper;
import org.dromara.stock.service.IAttentionStockService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 关注分组明细Service业务层处理
 *
 * @author dongfs
 * @date 2024-06-30
 */
@RequiredArgsConstructor
@Service
public class AttentionStockServiceImpl implements IAttentionStockService {

    private final AttentionStockMapper baseMapper;

    /**
     * 查询关注分组明细
     */
    @Override
    public AttentionStockVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询关注分组明细列表
     */
    @Override
    public TableDataInfo<AttentionStockVo> queryPageList(AttentionStockBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AttentionStock> lqw = buildQueryWrapper(bo);
        Page<AttentionStockVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询关注分组明细列表
     */
    @Override
    public List<AttentionStockVo> queryList(AttentionStockBo bo) {
        LambdaQueryWrapper<AttentionStock> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AttentionStock> buildQueryWrapper(AttentionStockBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AttentionStock> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAttentionId() != null, AttentionStock::getAttentionId, bo.getAttentionId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), AttentionStock::getCode, bo.getCode());
        return lqw;
    }

    /**
     * 新增关注分组明细
     */
    @Override
    public Boolean insertByBo(AttentionStockBo bo) {
        AttentionStock add = MapstructUtils.convert(bo, AttentionStock.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改关注分组明细
     */
    @Override
    public Boolean updateByBo(AttentionStockBo bo) {
        AttentionStock update = MapstructUtils.convert(bo, AttentionStock.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AttentionStock entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除关注分组明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
